package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;
import java.util.List;

@Cmd(code = Protocol.S_ZS_PROJECT_FINISH_NOTICE, desc = "招商项目结束通知")
public class ZsProjectFinishNoticeFromCross implements Command {

    private int templateId;

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        ZhaoShangProto.CrossZsProjectFinishSyncMsg respMsg = ZhaoShangProto.CrossZsProjectFinishSyncMsg.parseFrom(packet.getBytes());
        templateId = respMsg.getTemplateId();
        List<ZhaoShangProto.CrossZsProjectFinishSeatUserTemp> userList = respMsg.getUserList();
        for (ZhaoShangProto.CrossZsProjectFinishSeatUserTemp seatUserTemp : userList) {
            long serverId = seatUserTemp.getServerId();
            long userId = seatUserTemp.getUserId();
            if (serverId == GameServer.getInstance().getServerId()) {
                String language = UserMgr.getLanguage(userId);
                String projectName = MultipleLanguageMgr.getContent("Zs_Project_Name-" + templateId, language);
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.ZHAOSHANG_END_TITLE, language);
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ZHAOSHANG_END_CONTENT, language), projectName);
                MailManager.sendMail(userId, eMailType.DAILY.getValue(), "", mailContent, mailTitle);
            }
        }
    }
}
